//
//  ReadDetailsViewController.m
//  aMoment
//
//  Created by dllo on 15/10/11.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "ReadDetailsViewController.h"
#import "AllHeaderFiles.h"

@interface ReadDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)UICollectionView *itemCollectionView;
@property(nonatomic, retain)NSArray *itemArray;
@property(nonatomic, assign)NSInteger flag;
@end

@implementation ReadDetailsViewController

- (void)dealloc
{
    [_collectionView release];
    [_itemCollectionView release];
    [_itemArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = _name;
    self.dataSourceArray = [NSMutableArray arrayWithObjects:BODYSTRING_READ_DETAILS_HOT, BODYSTRING_READ_DETAILS_NEW, nil];
    self.itemArray = [NSArray arrayWithObjects:@"最热", @"最新", nil];

    
    
//    [self getData];
//    NSString *hotBodyString = [NSString stringWithFormat:@"start=0&limit=10&client=2&typeid=%@&sort=hot", _type];
//    NSString *newBodyString = [NSString stringWithFormat:@"start=0&limit=10&client=2&typeid=%@&sort=addtime", _type];
}

- (void)loadView
{
    [super loadView];
    //阅读内容collection
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 40 * FIT_HEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40 * FIT_HEIGHT, flowLayout.itemSize.width, flowLayout.itemSize.height) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[ReadDetailsCollectionCell class] forCellWithReuseIdentifier:@"content"];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
    //标签collection
    UICollectionViewFlowLayout *itemFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    itemFlowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 2, _collectionView.Y);
    itemFlowLayout.minimumInteritemSpacing = 0;
    itemFlowLayout.minimumLineSpacing = 0;
    
    self.itemCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, itemFlowLayout.itemSize.height) collectionViewLayout:itemFlowLayout];
    _itemCollectionView.dataSource = self;
    _itemCollectionView.delegate = self;
    _itemCollectionView.backgroundColor = [UIColor whiteColor];
    [_itemCollectionView registerClass:[ReadDetailsCollectionCellOfItem class] forCellWithReuseIdentifier:@"item"];
    [self.view addSubview:_itemCollectionView];
    [_itemCollectionView release];
}

#pragma mark cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

#pragma mark cell样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _itemCollectionView) {
        ReadDetailsCollectionCellOfItem *itemCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
        itemCell.itemLabel.text = _itemArray[indexPath.row];
        if (_flag == indexPath.row) {
            itemCell.itemLabel.backgroundColor = [UIColor lightGrayColor];
            itemCell.itemLabel.textColor = [UIColor whiteColor];
        } else {
            itemCell.itemLabel.backgroundColor = [UIColor whiteColor];
            itemCell.itemLabel.textColor = [UIColor blackColor];
        }
        return itemCell;
    }
    ReadDetailsCollectionCell *contentCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"content" forIndexPath:indexPath];
//    contentCell.dataSourceArray = [NSMutableArray array];
    contentCell.start = 0;
    contentCell.bodyString = self.dataSourceArray[indexPath.row];
    return contentCell;
}

#pragma mark cell点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _itemCollectionView) {
        CGFloat offsetX = 0;
        if (indexPath.row == 1) {
            offsetX = SCREEN_WIDTH;
        }
        _collectionView.contentOffset = CGPointMake(offsetX, 0);
        _flag = indexPath.row;
        [_itemCollectionView reloadData];
    }
}

#pragma mark scrollView协议方法(滚动结束)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetX = _collectionView.contentOffset.x / SCREEN_WIDTH;
    if (offsetX) {
        _flag = 1;
    } else {
        _flag = 0;
    }
    [_itemCollectionView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
